﻿window.crucible.treeMakerViewModel = (function (ko, datacontext) {
    var visibleNodes = ko.observableArray(),
        surrogateNode = new crucible.SurrogateNode(),
        //selectedSource = ko.observable(),
        canvas = new crucible.ArgumentCanvas(),
        form = new crucible.AssertionForm(canvas),
        detail = new crucible.AssertionView(),
        addIfNew = function(node) {
            if (!(visibleNodes.indexOf(node) >= 0)) {
                visibleNodes.push(node);
                return true;
            }
            return false;
        },
        removeNode = function(node) {
            return visibleNodes.remove(node);
        },
        removeFloaters = function(whiteList) {
            return visibleNodes.remove(function(item) {
                return whiteList.indexOf(item) < 0;
            });
        },
        focusNode = function(node) {
            var curr = surrogateNode;
            if (node.cloneOf) {
                node = node.cloneOf;
            }

            if (node === curr.original) return;

            if (curr && curr.original && curr.original.el) {
                curr.original.el.removeClass("focusedNode", 200);
                curr.original.IsFocused(false);
            }
            if (!curr || curr.Id !== node.Id) {
                if (curr && curr.IsEditing()) {
                    form.finishEditing();
                }

                //node.decorateSurrogate(surrogateNode); // push values of the new node into the surrogate
                surrogateNode.adopt(node);
                //surrogateNode.IsFocused(true);
                node.IsFocused(true);
                node.el.addClass("focusedNode", 200);

                if (!node.Published()) {
                    if (!form.initialized) {
                        form.init();
                    }

                    form.startEditing(surrogateNode);
                } else {
                    detail.loadNode(surrogateNode);
                }

                canvas.draw();
            }
        },
        saveDraft = function() {
            form.saveDraft();
        },
        publish = function(node) {
            form.publish(node);
        },
        startCanvas = function() {
            canvas.init();
            canvas.draw();
        },
        drawCanvas = function() {
            canvas.draw();
            return true;
        },
        addRoot = function(node, event) {
            form.triggerSearch(form, event);
            $.pnotify({ title: "Ctrl + Space" });
        },
        setSource = function(node) {
            form.triggerSourceSearch(form, event);
        },
        clearSource = function(node) {
            node.Source(null);
            drawCanvas();
        },
        reRoot = function(node) {
            crucible.nodeManager.reRoot(node.Id, surrogateNode, function (newParent) {
                crucible.datacontext.hydrateNode(surrogateNode.original);
                canvas.draw();
                focusNode(newParent);
            });
        },
        saveComment = function (node, comment) {
            var commentId = null;
            var body = null;
            if (comment) {
                commentId = comment.Id;
                body = comment.newReply();
            } else {
                body = node.newComment();
            }

            datacontext.saveComment(node.Id, body, commentId, function (response) {
                $.pnotify({ title: "Comment success", text: response });
                detail.loadNode(node);
            });
            
            if (!comment) {
                node.newComment("");
                node.CommentBoxVisible(false);
            }
            
        },
        deleteComment = function (comment) {
            comment.IsDeleting(false);
            datacontext.deleteComment(comment.Id, function() {
                detail.loadNode(surrogateNode);
            });
        };
        //editSource = function(source) {
        //    self.selectedSource(source);
        //},
        //addSource = function() {
        //    self.selectedSource(new Source());
        //},
        //sourceSave = function() {
        //    var selected = self.selectedSource();
        //    selected.accept();
        
        //    if (!selected.Id) {
        //        // ajax time to get that id

        //        //self.productList.push(new Product(selected.id(), selected.name()));   
        //    }

        //    self.selectedProduct("");
        //},
        //sourceCancel = function() {
        //    self.selectedProduct().cancel();
        //    self.selectedProduct("");
        //};

    


    return {
        visibleNodes: visibleNodes,
        surrogateNode: surrogateNode,
        addIfNew: addIfNew,
        removeNode: removeNode,
        focusNode: focusNode,
        saveDraft: saveDraft,
        publish: publish,
        startCanvas: startCanvas,
        drawCanvas: drawCanvas,
        addRoot: addRoot,
        setSource: setSource,
        clearSource: clearSource,
        removeFloaters: removeFloaters,
        reRoot: reRoot,
        saveComment: saveComment,
        deleteComment: deleteComment
        //editSource: editSource,
        //addSource: addSource,
        //sourceSave: sourceSave,
        //sourceCancel: sourceCancel
    };

})(ko, crucible.datacontext);


$(document).ready(function () {
    
    ko.applyBindings(window.crucible.treeMakerViewModel);
    crucible.nodeManager = new crucible.NodeManager();
    crucible.nodeManager.init();
    window.crucible.treeMakerViewModel.startCanvas();
    window.crucible.treeMakerViewModel.focusNode(crucible.nodeManager.rootNode);
    //window.crucible.treeMakerViewModel.initForm();
});
